﻿using System;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Tssipl.PaymentAdapter.Core;
namespace Tssipl.PaymentAdapter.MessageFormat.Swift
{

    public class SwiftBlock4 : IBlock
    {
        private List<Field> _fields = new List<Field>();

        /** Package-private constructor that parses the fields out of the 
         * block 4 contents (excluding {4: and final -}).
         * 
         * @param Contents the contents of block 4.
         */
        public SwiftBlock4(String Contents)
        {
            if (Contents.Length <= 4)
                return;

            try
            {
                //HACK BufferedReader reader = new BufferedReader(new StringReader(Contents));            
                //StreamReader reader = new StreamReader(new StringReader(Contents));
                StringReader reader = new StringReader(Contents);
                String tag = null;
                StringBuilder value = new StringBuilder(1024);
                while (true)
                {
                    String line = reader.ReadLine();
                    if (line == null)
                        break;

                    if (line.StartsWith(":"))
                    {
                        if (tag != null)
                            _fields.Add(new Field(tag, value.ToString()));

                        line = line.Substring(1);
                        value = new StringBuilder(1024);

                        int index = line.IndexOf(":");
                        if (index < 0)
                            tag = null;
                        else
                        {
                            tag = line.SubstringJava(0, index);
                            if (index < line.Length)
                                value.Append(line.Substring(index + 1));
                        }
                    }
                    else
                    {
                        value.Append("\r\n");
                        value.Append(line);
                    }
                }

                if (tag != null)
                    _fields.Add(new Field(tag, value.ToString()));
            }
            catch (Exception ee)
            {
            }
        }


        /** Retrieves a list of all fields in the block.
         * 
         * @return a list of the fields.
         */
        public List<Field> getFields()
        {
            return _fields;
        }


        /** Attempts to retrieve the FIRST field matching the field type code.
         * 
         * @param FieldType The type code for the field.
         * @return The contents of a matching field or null if not found.
         */
        public String getField(String FieldType)
        {
            //for (Field field : _fields)
            foreach (Field field in _fields)
            {
                if (field.getTag().CompareTo(FieldType) == 0)
                    return field.getValue();
            }
            return null;
        }


        /**
         * Attempts to retrieve all fields matching the field type code. Some SWIFT messages repeat field elements.
         * 
         * @param FieldType The type code for the field.
         * @return An array of 0 or more Strings containing the contents of the field(s).
         */
        public String[] getFields(String FieldType)
        {
            List<String> matches = new List<String>(4);
            //for (Field field : _fields)
            foreach (Field field in _fields)
            {
                if (field.getTag().CompareTo(FieldType) == 0)
                    matches.Add(field.getValue());
            }

            return matches.ToArray();//HACK new String[matches.size()]);
        }


        /** Returns a properly formatted block 4 including braces and 4: identifier.
         * 
         * @return a complete block 4.
         */
        //@Override
        public override String ToString()
        {
            return getFormattedBlock();
        }


        /* (non-Javadoc)
         * @see com.chrislaforetsoftware.swift.message.ISwiftBlock#getContents()
         */
        public String getContents()
        {
            StringBuilder sb = new StringBuilder(2048);
            //for (Field field : _fields)
            foreach (Field field in _fields)
            {
                if (sb.Length > 0)
                    sb.Append("\r\n");
                sb.Append(":");
                sb.Append(field.getTag());
                sb.Append(":");
                sb.Append(field.getValue());
            }
            return sb.ToString();
        }


        /* (non-Javadoc)
         * @see com.chrislaforetsoftware.swift.message.ISwiftBlock#getFormattedBlock()
         */
        public String getFormattedBlock()
        {
            StringBuilder sb = new StringBuilder(2048);
            sb.Append("{4:\r\n");
            sb.Append(getContents());
            sb.Append("\r\n-}");
            return sb.ToString();
        }
    }
}